package com.musiclibrary.scraper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.musiclibrary.common.JsoupUtils;
import com.musiclibrary.persistence.MusicAlbums;
import com.musiclibrary.persistence.Songs;

public class MusicAlbumParser {
	
	public MusicAlbums parseMusicAlbums(Element albumsElement,WebPage<Map<String,String>> webPage,String categoryTitle){
		MusicAlbums musicAlbums=null;
		try{
			if(albumsElement!=null){
				musicAlbums=new MusicAlbums();
				Element tagElement= albumsElement.select("a").first();
				String href = tagElement.attr("href");
				String albumTitle = tagElement.html();
				
				musicAlbums.setAlbumName(albumTitle);
				musicAlbums.setAlbumUrl(href);
				
				//update web page URL to fetch song list from site
				webPage.setUrl("http://telugump3downloads.net/music/index.php");
				//update web page form parameter
				HashMap<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("dir", categoryTitle+"/"+albumTitle);
				hashMap.put("p", "1");
				hashMap.put("sort", "0");
				webPage.getFormHandler().updateForm(hashMap);
							
				//fetch song list from site
				List<Songs> songList = getSongList(webPage, musicAlbums,categoryTitle);
				musicAlbums.setSongs(songList);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return musicAlbums;
	}

	private List<Songs> getSongList(WebPage<Map<String,String>> webPage,MusicAlbums musicAlbums, String categoryTitle){
		List<Songs> songList=null;
		try{
			Document document = new JsoupUtils().loadPage(webPage);
			if(document!=null){
				Elements elements = document.select("body div.bg");
				if(elements!=null && elements.size()>0){
					songList=new ArrayList<Songs>();
					for(Element element:elements){
						Songs song = parseSongs(element,musicAlbums.getAlbumName(),categoryTitle,webPage);
						song.setMusicAlbums(musicAlbums);
						songList.add(song);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return songList;
	}
	
	private Songs parseSongs(Element element, String albumTitle, String categoryTitle, WebPage<Map<String,String>> webPage){
		Songs songs=null;
		try{

			Element tagElement = element.select("a").first();
			String songUrl = tagElement.attr("href");
			String songName = tagElement.html();

			if (songName.contains("mp3")) {
				
				//update web page URL to fetch song from site
				webPage.setUrl("http://telugump3downloads.net/music/file.php");
				
				//update web page form parameter
				Map<String, String> map = new HashMap<String, String>();
				map.put("p", "1");
				map.put("sort", "0");
				map.put("file", "load/" + categoryTitle + "/" + albumTitle + "/" + songName);
				webPage.getFormHandler().updateForm(map);
				//update form parameter end
				
				//fetch data from site
				Document document = new JsoupUtils().loadPage(webPage);				
				
				//parse document 
				if (document != null) {
					songs = new Songs();					
					//read song download link from document
					Elements songElements = document.select("body div.fot strong a");
					Element downloadLinkElement = songElements.first();
					songUrl = downloadLinkElement.attr("href");
					
					songs.setSongUrl(songUrl);
					songs.setSongName(songName);
					System.out.println(songUrl);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return songs;
	}

}
